package com.yaorange.jk.web.sysadmin;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ModelDriven;
import com.yaorange.jk.entity.Dept;
import com.yaorange.jk.entity.Module;
import com.yaorange.jk.entity.Role;
import com.yaorange.jk.entity.ZtreeVO;
import com.yaorange.jk.service.DeptService;
import com.yaorange.jk.service.ModuleService;
import com.yaorange.jk.service.RoleService;
import com.yaorange.jk.utils.Pagination;
import com.yaorange.jk.web.BaseAction;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author coach tam
 * @date 2017/11/1
 */
public class RoleAction extends BaseAction implements ModelDriven<Role> {

    
    private Role model = new Role();

    //分页对象
    private Pagination page = new Pagination();

    //用户列表
    private List<Role> roleList;
    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;
    //接收选中模块的id 字符串 ，以逗号分隔
    private String moduleIds;
    /**
     * 分页查询
     */
    public String list() {
        page = roleService.findPage(page.getPageNo(), page.getPageSize());
        page.setUrl("roleAction_list");
        push(page);
        return "list";
    }

    /**
     * 跳转添加页面
     */
    public String tocreate() {

        roleList = roleService.findAll();
        return "tocreate";
    }

    /**
     * 添加
     */
    public String insert() {

        roleService.save(model);
        return "rlist";
    }

    /**
     * 跳转更新页面
     * @return
     */
    public String toupdate() {
        //根据id查询部门老数据
        model = roleService.findById(model.getId());
        //存入栈顶
        push(model);


        return "toupdate";
    }

    /**
     *更新
     */
    public String update() {
        roleService.update(model);
        return "rlist";
    }

    /**
     * 批量删除
     */
    public String delete() {
        String[] ids = model.getId().split(", ");
        roleService.deleteByIds(ids);
        return "rlist";
    }

    public String toview(){
        model = roleService.findById(model.getId());
        push(model);
        return "toview";
    }

    public String tomodule(){
        model = roleService.findById(model.getId());
        //将操作的角色名压到值栈顶
        push(model);
        return "tomodule";
    }

    public String roleModuleJsonStr() throws IOException {
        //先获取所有模块信息
        List<Module> allList = moduleService.findAll();
        //获取该角色所拥有的模块信息
        Role role = roleService.findById(model.getId());
        Set<Module> roleModuleSet = role.getModuleSet();
        //构建返回json数据的实体集
        Set<ZtreeVO> tree  = new HashSet<>(allList.size());
        for (int i = 0;i<allList.size();i++){
            Module module = allList.get(i);
            ZtreeVO ztreeVO = new ZtreeVO(module.getId(), module.getParentId(), module.getName(), false, true);
            //判断该模块是否 是 该角色已经拥有的模块
            if(roleModuleSet.contains(module)){
                ztreeVO.setChecked(true);
            }
            tree.add(ztreeVO);
        }
        //使用fastjson 将其转化成 json 字符串 响应给客户端
        String jsonTree = JSON.toJSONString(tree, true);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonTree);
        return NONE;
    }

    public String module(){
        //获取选中模块的id 数组
        String[] module_ids = moduleIds.split(",");
        //存储选中的模块实体
        Set<Module> moduleSet = new HashSet<>(module_ids.length);
        for (int i = 0; i <module_ids.length ; i++) {
            Module m = moduleService.findById(module_ids[i]);
            moduleSet.add(m);
        }
        //拿到要操作角色的持久化对象
        Role opra_role = roleService.findById(model.getId());
        //更新该role模块信息
        opra_role.setModuleSet(moduleSet);
        roleService.savaOrUpdate(opra_role);
        return "rlist";
    }

    public String getModuleIds() {
        return moduleIds;
    }

    public void setModuleIds(String moduleIds) {
        this.moduleIds = moduleIds;
    }

    public void setPage(Pagination page) {
        this.page = page;
    }

    public List<Role> getRoleList() {
        return roleList;
    }

    @Override
    public Role getModel() {
        return model;
    }
}
